/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.board;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;

import org.mopore.jat.util.ImageService;

public class WatermarkPainter {

    private static final String DARK_BIG_PATH = "/images/watermarks/mopore-big-dark.gif";

    private static final String BRIGHT_BIG_PATH = "/images/watermarks/mopore-big-bright.gif";

    private static final String BRIGHT_SMALL_PATH = "/images/watermarks/mopore-small-bright.gif";

    private static final String DARK_SMALL_PATH = "/images/watermarks/mopore-small-dark.gif";

    private BufferedImage watermarkImage;

    private Point paintPoint;

    public enum LogoSize {
        BIG, SMALL
    }

    public LogoSize logoSize;

    private String correctSizedLogo;

    private BufferedImage correctSizedWaterMark;

    public WatermarkPainter(LogoSize logoSize) {
        watermarkImage = null;
        paintPoint = null;
        this.logoSize = logoSize;
    }

    public void paintWatermark(final Graphics2D g2, final Composite origComposite,
            BufferedImage screenImage) {

        float alpha = .12f;
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));

        if (correctSizedLogo == null && correctSizedWaterMark == null) {
            correctSizedLogo = (logoSize == LogoSize.BIG) ? DARK_BIG_PATH : DARK_SMALL_PATH;
            correctSizedWaterMark = ImageService.load(correctSizedLogo);
        }

        if (watermarkImage == null) {
            boolean decisionForBright = useBrightImage(screenImage, correctSizedWaterMark);
            String path;
            if (logoSize == LogoSize.BIG) {
                path = (decisionForBright) ? BRIGHT_BIG_PATH : DARK_BIG_PATH;
            } else {
                path = (decisionForBright) ? BRIGHT_SMALL_PATH : DARK_SMALL_PATH;
            }
            watermarkImage = ImageService.load(path);
        }

        Point orig = getPaintPoint(watermarkImage, screenImage);

        int x = (int) orig.getX();
        int y = (int) orig.getY();
        g2.drawImage(watermarkImage, x, y, null);

        g2.setComposite(origComposite);
    }

    private Point getPaintPoint(BufferedImage waterImage, BufferedImage screenImage) {
        if (paintPoint == null)
            paintPoint = new Point(getImageHeight(), getImageHeight());

        return paintPoint;
    }

    private boolean useBrightImage(BufferedImage screenImage, BufferedImage correctSizedLogo) {
        final int thresholdValue = 500;
        int brightPixel = 0;
        int darkPixel = 0;

        int screenWidth = screenImage.getWidth();
        int screenHeight = screenImage.getHeight();
        int logoWidth = correctSizedLogo.getWidth();
        int logoHeight = correctSizedLogo.getHeight();

        // screenImage smaller than logo
        if ((screenWidth < logoWidth) || (screenHeight < logoHeight)) {
            return false;
        }

        Point orig = getPaintPoint(correctSizedLogo, screenImage);
        int pointX = orig.x;
        int pointY = orig.y;

        // for now, every 4th pixel is taken into account
        for (int i = pointX; i < pointX + logoWidth; i += 4) {
            for (int j = pointY; j < logoHeight + pointY; j++) {
                int value = screenImage.getRGB(i, j);
                int r = (0xff0000 & value) >> 16;
                int g = (0xff00 & value) >> 8;
                int b = (0xff & value);
                if (r + g + b >= thresholdValue) {
                    brightPixel++;
                } else {
                    darkPixel++;
                }
            }
        }

        boolean result = (brightPixel > darkPixel) ? false : true;
        return result;
    }

    private int getImageHeight() {

        if (logoSize == LogoSize.BIG) {
            BufferedImage temp = ImageService.load(DARK_BIG_PATH);
            return temp.getHeight();
        } else {
            BufferedImage temp = ImageService.load(DARK_SMALL_PATH);
            return temp.getHeight();
        }
    }

}
